package cn.epsilon3.media.ses.tencent.service.impl;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.epsilon3.configBean.InitializingWarnSesConfig;
import cn.epsilon3.exception.Epsilon3Exception;
import cn.epsilon3.media.ses.tencent.service.TencentSesWarnClient;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ses.v20201002.SesClient;
import com.tencentcloudapi.ses.v20201002.models.SendEmailRequest;
import com.tencentcloudapi.ses.v20201002.models.Template;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 腾讯云邮件服务器
 *
 * @author epsilon3
 * @date 2023-05-14
 */
@Service
public class TencentSesWarnClientImpl implements TencentSesWarnClient {

    private static final Log log = LogFactory.get();

    @Resource
    InitializingWarnSesConfig initializingWarnSesConfig;


    @Override
    public void exceptionWarnEmail(String projectName,
                                   String uuid,
                                   String message) {
        if (initializingWarnSesConfig.getEnable()) {
            String[] warnSendEmail = initializingWarnSesConfig.getWarnSendEmail();
            if (0 != warnSendEmail.length) {
                Credential cred = getCredential();
                HttpProfile httpProfile = new HttpProfile();
                ClientProfile clientProfile = new ClientProfile();
                clientProfile.setHttpProfile(httpProfile);
                SesClient client = new SesClient(cred, initializingWarnSesConfig.getRegion(), clientProfile);
                SendEmailRequest req = new SendEmailRequest();
                req.setFromEmailAddress(initializingWarnSesConfig.getEmailAddress());
                req.setDestination(warnSendEmail);
                Template template1 = new Template();
                template1.setTemplateID(initializingWarnSesConfig.getTemplateID());
                JSONObject sendParam = new JSONObject(16);
                sendParam.put("project", projectName);
                sendParam.put("uuid", uuid);
                sendParam.put("message", message);
                template1.setTemplateData(sendParam.toJSONString());
                req.setTemplate(template1);
                req.setSubject(projectName);
                log.info(JSON.toJSONString(req));
                try {
                    client.SendEmail(req);
                } catch (TencentCloudSDKException tencentCloudSDKException) {
                    throw new Epsilon3Exception("tencent ses send exception：" + tencentCloudSDKException.getMessage());
                }
            }
        }

    }

    private Credential getCredential() {
        return new Credential(initializingWarnSesConfig.getSecretId(), initializingWarnSesConfig.getSecretKey());
    }
}
